﻿function grid_agent_add() {
    //location.href =decodeURIComponent("/User/UserAdd") ;
    grid_agent_show();
}

function save_agent() {
    //addDialog.frame.save();
    $("#lblmsg").html('');
    var form = $('#frmAgent');
    form.submit();
}

function grid_agent_edit() {
    if (membergrid.getSelecteds().length == 0) {
        $.ligerDialog.warn('至少选择一个经纪人进行修改！', '温馨提示');
        return;
    }
    var agent = membergrid.getSelected();
    grid_agent_show(agent.UserName, agent.RealName, agent.HandPhone, agent.IDCardNo, agent.Address, agent.AgentInfo);
}
function grid_agent_rowedit(userName, realName, handphone, idcardno, address, settlementtype, settlement) {
    grid_agent_show(userName, realName, handphone, idcardno, address, {SettlementType:settlementtype, Settlement:settlement});
}
function grid_agent_show(userName, realName, handphone, idcardno, address,agentInfo) {
    var title = "新增经纪人";
    $('#txtAgentName').focus();
    if (userName != null) {
        title = "修改经纪人【 " + realName + " 】"
        agent_set(userName, realName, handphone, idcardno, address, agentInfo);
        $('#txtRealName').focus();
    } else {
        agent_reset();
    }
    saveDialog = $.ligerDialog.open({
        title: title,
        id: 'agentDialog',
        name: 'agentDialog',
        slide: false,
        isHidden: false,
        modal: true,
        target: $("#frmAgent"), 
        width: 600, isResize: true, allowClose: false,
        buttons: [
                    { text: '保存', onclick: save_agent },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            agent_reset();
                        }
                    }
                ]
    });
}
function agent_set(username, realName, handphone, idcardno, address, agentInfo) {

    $(".l-text-field").ligerHideTip();
    liger.get("txtAgentName").setDisabled();
    $("#txtAgentName").val(username);
    $("#txtRealName").val(realName);
    $("#txtAgentHandPhone").val(handphone);
    settlementType.setValue(agentInfo.SettlementType);
    $("#txtSettlement").val(agentInfo.Settlement);
    //settlementType.setValue(0);
     //liger.get("settlementType").setValue(0);
    $("#txtAgentIDNO").val(idcardno);
    $("#txtAddress").val(address);
    $("#isEdit").val("1");
    $("#lblmsg").html("");
}


function agent_reset() {
    $(".l-text-field").ligerHideTip();
    liger.get("txtAgentName").setEnabled();
    $("#txtAgentName").val("");
    $("#txtRealName").val("");
    $("#txtAgentHandPhone").val("");
    $("#txtAgentIDNO").val("");
    $("#txtSettlement").val("");
    settlementType.setValue(0); 
    $("#txtSettlement").val("");
    $("#txtAddress").val("");
    $("#isEdit").val("0");
    $("#lblmsg").html("");
}

function grid_agent_delete() {
    var userNames = "";
    var names = "";


    var selected = membergrid.getSelecteds();
    if (selected.length == 0) {
        $.ligerDialog.warn('请选择要删除的数据！', '温馨提示');
        return;
    }

    for (var i = 0; i < selected.length; i++) {
        userNames += selected[i].UserName + ",";
        names += selected[i].RealName + ",";
    }
    names = names.substring(0, names.length - 1);
    userNames = userNames.substring(0, userNames.length - 1);
    agent_delete(userNames, names);

}

function agent_delete(userName, realName) {
    $.ligerDialog.confirm('确定删除经纪人[' + realName + ']吗？', '温馨提示', function (result) {
        if (result) {
            $.ajax({
                url: '/Member/AgentDelete',
                data: "userNames=" + userName,
                type: "post",
                success: function (result) {
                    if (result == "1") { 
                        $.ligerDialog.success("成功删除经济人[" + realName + "]。");
                        search();
                    } else {
                        $.ligerDialog.error('删除失败！', '温馨提示');
                    }
                }
            });
        }
    });
}

function ViewBaseDetail(userName, realName) {
    $.ajax({
        url: '/Member/GetAgentByUserName',
        data: "userName=" + userName,
        type: "post",
        success: function (result) {
            //result.RID,result.Data

            result = result.replace(/:null/g, ":\"\"");
            result = eval("(" + result + ")");
            if (result.RID == "1") {
                var mtype = '经纪人';
                var active = result.Data.IsActive ? '已激活' : '未激活';
                var status = result.Data.Status == 1 ? '正常' : '已禁用';
                var settlementType = result.Data.AgentInfo.SettlementType == 0 ? "按比例" : "固定值";
                var settlementvalue = result.Data.AgentInfo.SettlementType == 0 ? result.Data.AgentInfo.Settlement + "%" : result.Data.AgentInfo.Settlement;
                var settlement = settlementvalue+"/笔";
                var gender = '未知';
                if (result.Data.Gender == 0) {
                    gender = '男';
                }
                else if (result.Data.Gender == 1) {
                    gender = '女';
                }
                var checkhtml = "";
                if (result.Data.IDCardCheck) {
                    checkhtml += '<img alt="身份证已认证" src="/content/images/id.gif"/>';
                }
                else {
                    checkhtml += '<img alt="身份证未认证" src="/content/images/id_0.gif"/>';
                }

                if (result.Data.HandPhoneCheck) {
                    checkhtml += '<img alt="手机已认证" src="/content/images/phone.gif"/>';
                }
                else {
                    checkhtml += '<img alt="手机未认证" src="/content/images/phone_0.gif"/>';
                }
                if (result.Data.EmailCheck) {
                    checkhtml += '<img alt="邮箱已认证" src="/content/images/email.gif"/>';
                }
                else {
                    checkhtml += '<img alt="邮箱未认证" src="/content/images/email_0.gif"/>';
                }

                var html = '<table cellpadding="0" cellspacing="0" class="l-table-edit"><tr>'
                     + '<td align="right" class="l-table-edit-td">经纪人代码:</td><td align="left" class="l-table-edit-td" colspan="3">' + result.Data.AgentInfo.AgentCode + '</td>' 
                       + '</tr><tr>'
                        + '<td align="right" class="l-table-edit-td">用户名:</td><td align="left" class="l-table-edit-td">' + result.Data.UserName + '</td>'
                        + '<td align="right" class="l-table-edit-td">认证情况:</td><td align="left" class="l-table-edit-td">' + checkhtml + '</td>'
                        + '</tr><tr>'
                        + '<td align="right" class="l-table-edit-td">真实姓名:</td><td align="left" class="l-table-edit-td">' + result.Data.RealName + '</td>'
                        + '<td align="right" class="l-table-edit-td">会员类型:</td><td align="left" class="l-table-edit-td">' + mtype + '</td>'
                         + '</tr><tr>'
                          + '<td align="right" class="l-table-edit-td">性别:</td><td align="left" class="l-table-edit-td">' + gender + '</td>'
                        + '<td align="right" class="l-table-edit-td">注册IP:</td><td align="left" class="l-table-edit-td">' + result.Data.RegisterIP + '</td>'
                         + '</tr><tr>'
                          + '<td align="right" class="l-table-edit-td">结算方式:</td><td align="left" class="l-table-edit-td">' + settlementType + '</td>'
                        + '<td align="right" class="l-table-edit-td">结算值:</td><td align="left" class="l-table-edit-td">' + settlement + '</td>'
                         + '</tr><tr>'
                         + '<td align="right" class="l-table-edit-td">移动电话:</td><td align="left" class="l-table-edit-td">' + result.Data.HandPhone + '</td>'
                        + '<td align="right" class="l-table-edit-td">身份证:</td><td align="left" class="l-table-edit-td">' + result.Data.IDCardNo + '</td>'
                          + '</tr><tr>'
                         + '<td align="right" class="l-table-edit-td">学历:</td><td align="left" class="l-table-edit-td">' + result.Data.Education + '</td>'
                        + '<td align="right" class="l-table-edit-td">邮箱:</td><td align="left" class="l-table-edit-td">' + result.Data.Email + '</td>'
                        + '</tr><tr>'
                         + '<td align="right" class="l-table-edit-td">婚姻:</td><td align="left" class="l-table-edit-td">' + result.Data.MaritalStatus + '</td>'
                        + '<td align="right" class="l-table-edit-td">居住地:</td><td align="left" class="l-table-edit-td">' + result.Data.Address + '</td>'
                         + '</tr><tr>'
                          + '<td align="right" class="l-table-edit-td">注册人:</td><td align="left" class="l-table-edit-td">' + result.Data.CreateUser + '</td>'
                        + '<td align="right" class="l-table-edit-td">注册时间:</td><td align="left" class="l-table-edit-td">' + result.Data.CreateTime.replace(/T/g, " ") + '</td>'
                         + '</tr><tr>'
                         + '<td align="right" class="l-table-edit-td">是否激活:</td><td align="left" class="l-table-edit-td">' + active + '</td>'
                        + '<td align="right" class="l-table-edit-td">当前状态:</td><td align="left" class="l-table-edit-td">' + status + '</td>'
                          + '</tr><tr>'
                          + '</table>';
                $.ligerDialog.open({
                    title: '经纪人【' + realName + '】的基本资料',
                    id: 'viewDialog',
                    name: 'viewDialog',
                    slide: false,
                    isHidden: false,
                    modal: true,
                    content: html,
                    width: 500, isResize: false, allowClose: false,
                    buttons: [
                            {
                                text: '关闭', onclick: function (item, dialog) {
                                    dialog.hide();
                                }
                            }
                        ]
                });

            } else {
                $.ligerDialog.warn("查看经纪人【" + realName + "】基本资料失败！", '温馨提示');
            }
        }
    });
}
